/**
* @project admin-go-api
* @name sysLogin
* @descriptions 登录相关服务
* @author 寒锋
* @date 2023-12-20 15:05:00
 */

package service

import (
	"admin-go-api/api/dao"
	"admin-go-api/api/entity"
	"admin-go-api/common/response"
	"admin-go-api/common/util"
	"admin-go-api/constants"
	"admin-go-api/pkg/jwt"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"sync"
)

// ISysLoginService 定义登录相关接口
type ISysLoginService interface {
	Login(c *gin.Context, dto entity.LoginDto)
}

type SysLoginServiceImpl struct {
}

func (s SysLoginServiceImpl) Login(c *gin.Context, dto entity.LoginDto) {
	// 登录参数效验
	err := validator.New().Struct(dto)
	if err != nil {
		response.Failed(c, int(constants.ApiCode.MissingLoginParameter), constants.ApiCode.GetMessage(constants.ApiCode.MissingLoginParameter))
		return
	}
	// 验证码是否过期
	captcha := util.RedisStore{}.Get(dto.IdKey, true)
	if len(captcha) == 0 {
		SysLoginInfoService().CreateSysLoginInfo(c, dto.Username, "验证码已过期", entity.LoginInfoStatusFailed)
		response.Failed(c, int(constants.ApiCode.VerificationCodeHasExpired), constants.ApiCode.GetMessage(constants.ApiCode.VerificationCodeHasExpired))
		return
	}
	// 效验验证码
	verifyResult := CaptchaVerify(dto.IdKey, dto.Captcha)
	if !verifyResult {
		SysLoginInfoService().CreateSysLoginInfo(c, dto.Username, "验证码不正确", entity.LoginInfoStatusFailed)
		response.Failed(c, int(constants.ApiCode.CaptchaNotTrue), constants.ApiCode.GetMessage(constants.ApiCode.CaptchaNotTrue))
		return
	}
	// 效验
	sysAdmin := dao.SysAdminDetailByLogin(dto)
	if sysAdmin.Password != util.EncryptionMd5(dto.Password) {
		SysLoginInfoService().CreateSysLoginInfo(c, dto.Username, "密码不正确", entity.LoginInfoStatusFailed)
		response.Failed(c, int(constants.ApiCode.PasswordNotTrue), constants.ApiCode.GetMessage(constants.ApiCode.PasswordNotTrue))
		return
	}
	if sysAdmin.Status == entity.AdminStatusForbidden {
		SysLoginInfoService().CreateSysLoginInfo(c, dto.Username, "账号已停用", entity.LoginInfoStatusFailed)
		response.Failed(c, int(constants.ApiCode.StatusIsEnable), constants.ApiCode.GetMessage(constants.ApiCode.StatusIsEnable))
		return
	}
	// 生成token
	tokenString, _ := jwt.GenerateTokenByAdmin(sysAdmin)
	var leftMenuVoList []entity.LeftMenuVo
	var stringList = make([]string, 0)
	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		// 左侧菜单列表
		leftMenuList := dao.QueryLeftMenuList(sysAdmin.AdminId)
		for _, value := range leftMenuList {
			menuSvoList := dao.QueryMenuVoList(sysAdmin.AdminId, value.MenuId)
			item := entity.LeftMenuVo{}
			item.MenuSvoList = menuSvoList
			item.MenuId = value.MenuId
			item.MenuName = value.MenuName
			item.Icon = value.Icon
			item.Url = value.Url
			leftMenuVoList = append(leftMenuVoList, item)
		}
		wg.Done()
	}()
	go func() {
		// 权限列表
		permissionList := dao.QueryPermissionList(sysAdmin.AdminId)
		for _, value := range permissionList {
			stringList = append(stringList, value.Value)
		}
		wg.Done()
	}()
	wg.Wait()
	data := map[string]interface{}{"token": tokenString, "sysAdmin": sysAdmin, "leftMenuList": leftMenuVoList, "permissionList": stringList}
	response.Success(c, data)
	SysLoginInfoService().CreateSysLoginInfo(c, dto.Username, "登录成功", entity.LoginInfoStatusNormal)
}

var sysLoginService = SysLoginServiceImpl{}

func SysLoginService() ISysLoginService {
	return &sysLoginService
}
